//
//  HomeWorkTimeVC.m
//  QoilPublic
//
//  Created by SunChao on 2018/4/15.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "HomeWorkTimeVC.h"
#import "SINavigationMenuView.h"
#import "WorkTimeVC.h"
#import "WorkDayVC.h"

#define btnWidth ([UIScreen mainScreen].bounds.size.width)/2
@interface HomeWorkTimeVC ()<SINavigationMenuDelegate,UIScrollViewDelegate>

{
    NSArray *_titleArr;
    NSMutableArray *_btnArr;
    //为了移除通知*********
    WorkTimeVC *_vc2;
    WorkDayVC *_vc3;
    
}
@property (nonatomic,strong)SINavigationMenuView *menu;
@property(nonatomic,strong)UIScrollView *bScrollView;

@property(nonatomic,strong)UIView *btitleView;

@property(nonatomic,strong)UIView *selectView;

@property (nonatomic,copy)NSString *dateString;

@end

@implementation HomeWorkTimeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestMonthData];
    self.dateString = @"2018-04";
    if (self.navigationItem) {
        CGRect frame = CGRectMake(0, 0, 200, self.navigationController.navigationBar.bounds.size.height);
        _menu = [[SINavigationMenuView alloc] initWithFrame:frame title:self.dateString];
        
        [_menu displayMenuInView:self.view];
      
        _menu.delegate = self;
        self.navigationItem.titleView = _menu;
    }
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _titleArr = @[@"月总工作时长",@"日总工作时长"];
    
    _btnArr = [NSMutableArray array];
    
    [self createUI];
    
    [self addtitleButton];
    
    [self addController];
    
    WorkTimeVC *vc = (WorkTimeVC *)[self.childViewControllers firstObject];
    
    vc.view.frame = self.bScrollView.bounds;
    
    [self.bScrollView addSubview:vc.view];
    
    self.view.clipsToBounds = YES;
    
}
- (void)createUI{
    WS(wSelf);
    
    //状态栏
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    NSLog(@"status width %f ---- status height %f",rectStatus.size.width, rectStatus.size.height);
    //导航栏
    CGRect rectNav = self.navigationController.navigationBar.frame;
    NSLog(@"nav width %f ---- nav height %f",rectNav.size.width, rectNav.size.height);
    
    [self.view addSubview:self.btitleView];
    
    [_btitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wSelf.view.mas_top).offset(rectStatus.size.height+ rectNav.size.height);
        make.left.equalTo(wSelf.view.mas_left).offset(0);
        make.right.equalTo(wSelf.view.mas_right).offset(0);
        make.height.equalTo(@45);
    }];
    self.selectView.frame = CGRectMake(0, 43, btnWidth, 2);
    [self.btitleView addSubview:self.selectView];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [QColorUtility defaultLineColor];
    [self.btitleView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wSelf.view.mas_left).offset(0);
        make.right.equalTo(wSelf.view.mas_right).offset(0);
        make.height.equalTo(@0.5);
        make.bottom.equalTo(wSelf.btitleView.mas_bottom).offset(0);
    }];
    
    [self.view addSubview:self.bScrollView];
    [_bScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_btitleView.mas_bottom).offset(0);
        make.left.equalTo(wSelf.view.mas_left).offset(0);
        make.right.equalTo(wSelf.view.mas_right).offset(0);
        make.bottom.equalTo(wSelf.view.mas_bottom).offset(0);
    }];
    self.bScrollView.contentSize = CGSizeMake(_titleArr.count*kScreenWidth, 0);
}


#pragma mark --- 添加待接单/配送中/已完成按钮
- (void)addtitleButton{
    for (int i=0; i<_titleArr.count; ++i) {
        UIButton *button = [QViewUtility createButtonWithFrame:CGRectMake(i*btnWidth,0, btnWidth,43) font:Ksize15 title:_titleArr[i] titleColor:[QColorUtility defaultBackColor] backgroundColor:[UIColor whiteColor]];
        button.tag = i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        if(button.tag == 0){
            [button setTitleColor:[QColorUtility defaultBlueColor] forState:UIControlStateNormal];
        }else{
            [button setTitleColor:[QColorUtility defaultBackColor] forState:UIControlStateNormal];
        }
        [self.btitleView addSubview:button];
        [_btnArr addObject:button];
    }
}


#pragma mark --- 添加控制器
- (void)addController{
    
    WorkTimeVC *vc2 = [[WorkTimeVC alloc] init];
    _vc2 = vc2;
    [self addChildViewController:vc2];
    
    WorkDayVC *vc3 = [[WorkDayVC alloc] init];
    _vc3 = vc3;
    
    [self addChildViewController:vc3];
}


#pragma mark ---点击配送中方法
- (void)scrollToReceivedOrder{
    
    UIButton *btn = _btnArr[0];
    [self buttonAction:btn];
}

#pragma mark ---点击已完成方法
- (void)scrollToFinishedOrder{
    
    UIButton *btn = _btnArr[2];
    [self buttonAction:btn];
    
}

#pragma mark ---点击投诉方法
- (void)callPhoneAction{
    
    [QViewUtility showPlayBoxView:PlayboxCallPhoneStyle myBlock:^(NSInteger index) {
        if(index == 1){
            [QViewUtility callPhone];
        }
    }];
}

#pragma mark --- scrollView位置偏移
- (void)buttonAction:(UIButton *)btn{
    CGFloat offsetX = btn.tag * self.bScrollView.frame.size.width;
    CGFloat offsetY = self.bScrollView.contentOffset.y;
    CGPoint offset = CGPointMake(offsetX, offsetY);
    [self.bScrollView setContentOffset:offset animated:YES];
}

#pragma mark --- scrollviewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    int index = scrollView.contentOffset.x/self.bScrollView.frame.size.width;
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationscrollsToTop object:nil userInfo:@{kTopKey:[NSString stringWithFormat:@"%d",index]}];
    UIViewController *vc = self.childViewControllers[index];
    [self setupButtonColor:index];
    if(vc.view.superview){
        return;
    }else{
        vc.view.frame = scrollView.bounds;
        [self.bScrollView addSubview:vc.view];
    }
}

//手势调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSUInteger index = scrollView.contentOffset.x/self.bScrollView.frame.size.width;
    [UIView animateWithDuration:0.23 animations:^{
        _selectView.frame = CGRectMake(index*btnWidth, 43, btnWidth, 2);
    }];
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

//滑动调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat index = (scrollView.contentOffset.x/self.bScrollView.frame.size.width);
    _selectView.frame = CGRectMake(btnWidth * index , 43, btnWidth, 2);
    CGFloat scrollViewLeftX = scrollView.contentOffset.x;
    if(scrollViewLeftX < 0){
        self.bScrollView.scrollEnabled = NO;
    }else{
        self.bScrollView.scrollEnabled = YES;
    }
}

//设置button颜色
- (void)setupButtonColor:(NSInteger)index{
    
    for (UIButton *btn in _btnArr) {
        if(btn.tag == index){
            [btn setTitleColor:[QColorUtility defaultBlueColor] forState:UIControlStateNormal];
        }else{
            [btn setTitleColor:[QColorUtility defaultBackColor] forState:UIControlStateNormal];
        }
    }
}

#pragma mark --- get/set
- (UIScrollView *)bScrollView{
    if (!_bScrollView) {
        _bScrollView = [[UIScrollView alloc] init];
        _bScrollView.showsHorizontalScrollIndicator = NO;
        _bScrollView.showsVerticalScrollIndicator = NO;
        _bScrollView.pagingEnabled = YES;
        _bScrollView.delegate = self;
        _bScrollView.scrollsToTop = NO;
    }
    return _bScrollView;
}

- (UIView *)btitleView{
    if (!_btitleView) {
        _btitleView = [[UIView alloc] init];
        _btitleView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.7];
    }
    return _btitleView;
}

- (UIView *)selectView{
    if (!_selectView) {
        _selectView = [[UIView alloc] init];
        _selectView.backgroundColor = [QColorUtility defaultBlueColor];
    }
    return _selectView;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:_vc2];
    [[NSNotificationCenter defaultCenter] removeObserver:_vc3];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark --SINavigationMenuDelegate
- (void)didSelectItemAtIndex:(NSUInteger)index
{
    NSString *selmenu = [_menu.items objectAtIndex:index];
    _menu.menuButton.title.text = selmenu;
//    self.dateString = selmenu;
    
      WorkDayVC *vc = [self.childViewControllers lastObject];
    
    vc.year = [selmenu substringToIndex:4];
    vc.month = [selmenu substringFromIndex:5];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"MonthSelectedNotification" object:selmenu];
    
}

- (void)requestMonthData{
    
    NSString *url = [QParamService urlWithIOTAction:@"report/monthList"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:[QParamService getToken] forKey:@"token"];
    
    [[QHttpManager sharedManager] POST:url parameterMD5jm:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if (error) {
            
            NSLog(@"error:%@",basModel.errMsg);
            
            return ;
        }
        
        if([basModel.result isEqualToString:@"success"]){
            
            NSLog(@"dic:%@",basModel.items);
            _menu.items = basModel.items;
        }
    }];
}
@end
